﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Tools;

namespace Punia
{
    [CommandAttribute(true)]
    public partial class alter_message_type : BaseForm
    {
        public alter_message_type()
            : base()
        {
        }

        public alter_message_type(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbMessage.DataSource = Utility.message_types(dbContext);
            cbMessage.DisplayMember = "Name";

            cbSchemas.DataSource = Utility.schema_collections(dbContext);
            cbSchemas.DisplayMember = "Name";
            cbSchemas.ValueMember = "schemat";
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("USE [" + dbContext.Database + "];");
            sb.AppendLine();
            if (cbMessage.SelectedIndex > 0)
                sb.AppendLine("ALTER MESSAGE TYPE [" + cbMessage.Text + "]");
            else
                sb.AppendLine("ALTER MESSAGE TYPE []");

            if (chValidation.Checked)
            {
                sb.AppendLine("VALIDATION = " + cbValidation.Text);
            }

            if ((string)cbValidation.SelectedItem == "VALID_XML WITH SCHEMA COLLECTION")
            {
                sb.AppendLine("   [" + cbSchemas.Text + "]");
            }

            tbCommand.Text = sb.ToString();
        }

        private void cbMessage_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void chValidation_CheckedChanged(object sender, EventArgs e)
        {
            cbValidation.Enabled = chValidation.Checked;
            Write();
        }

        private void cbValidation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((string)cbValidation.SelectedItem == "VALID_XML WITH SCHEMA COLLECTION")
            {
                cbSchemas.Enabled = true;
            }
            else cbSchemas.Enabled = false;

            Write();
        }

        private void cbSchemas_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void btnSchow_Click(object sender, EventArgs e)
        {
            if (cbSchemas.Text == string.Empty)
            {
                return;
            }

            try
            {
                string xmlSchema = Utility.xml_schema(dbContext, (string)cbSchemas.SelectedValue, cbSchemas.Text);

                if (xmlSchema != string.Empty)
                {
                    string fileName = mainForm.tempPath + Path.GetRandomFileName() + ".xml";
                    StreamWriter sw = File.CreateText(fileName);
                    sw.Write(xmlSchema);
                    sw.Close();

                    XmlBrowser xb = new XmlBrowser();
                    xb.browser.Navigate(fileName);
                    xb.Show();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }

        }

    }
}